
package laya.semi.process;

import java.io.*;

//import org.apache.log4j.*;

public class ProcessClassLoader extends ClassLoader
{
	//private static Logger log = Logger.getLogger(ProcessClassLoader.class);
	
	private String m_oClassRootPath = null;
	
	//private Map m_oProcessObjectTable = new HashMap();
	
	public ProcessClassLoader(String oClassRootPath, ClassLoader oParent)
	{
		super(oParent);
		
		if (null == oClassRootPath)
			throw new java.lang.IllegalArgumentException("oClassRootPath can not be null");
		
		m_oClassRootPath = oClassRootPath;
	}
	
	protected Class findClass(String oName)
		throws ClassNotFoundException
	{
		try
		{
			InputStream oSrc = new FileInputStream(classToPathname(oName) + ".class");
			byte[] vBuff = new byte[2048];
			int nCount = 0;
			int nOffset = 0;
			
			while(0 <= (nCount = oSrc.read(vBuff, nOffset, vBuff.length - nOffset)))
			{
				if ((nOffset += nCount) == vBuff.length)
				{
					byte[] tmp = vBuff;
					vBuff = new byte[vBuff.length * 2];
					
					for (int i = 0; i < tmp.length; ++i)
						vBuff[i] = tmp[i];
				}
			}
			
			oSrc.close();
			Class c = defineClass(oName, vBuff, 0, nOffset);
			resolveClass(c);
			return c;
		}
		catch(Exception e)
		{
			throw new ClassNotFoundException(oName + ": " + e.getMessage());
		}
	}

	private String classToPathname(String oName)
	{
		StringBuffer oClassPath = new StringBuffer(m_oClassRootPath);
		oClassPath.append("/").append(oName.replace('.', '/'));
		return oClassPath.toString();
	}
}
